﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using WPFExcelReport.ViweModels;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for SETUPABON.xaml
    /// </summary>
    ///
    public partial class ITXSETUP : DocumentBase
    {
        ApplicationBase ApplicationBase;
        bool IsSetUp = false;
        bool IsLoaded = false;

        BackgroundWorker bgWoker;
        public SETINTEXDB SetEnv
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetEnv");
            }
        }
        public SETINTEXDB SetMaster
        {
            get
            {
                return vm.SetMaster;
            }
        }
        public SETINTEXDB SetDetail
        {
            get
            {
                return vm.SetDetail;
            }
        }   
        #region=====vwMULLANG
        public CollectionViewSource vwMULLANG
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMULLANG");
            }
        }
        public DataRow dwMULLANG
        {
            get
            {
                if (vwMULLANG.View == null)
                    return null;
                if ((DataRowView)vwMULLANG.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMULLANG.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPMULLANG
        public CollectionViewSource vwTMPMULLANG
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPMULLANG");
            }
        }
        public DataRow dwTMPMULLANG
        {
            get
            {
                if (vwTMPMULLANG.View == null)
                    return null;
                if ((DataRowView)vwTMPMULLANG.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPMULLANG.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwHRSTAMM=======
        public CollectionViewSource vwHRSTAMM
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwHRSTAMM");
            }
        }
        public DataRow dwHRSTAMM
        {
            get
            {
                if (vwHRSTAMM.View == null)
                    return null;
                if ((DataRowView)vwHRSTAMM.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwHRSTAMM.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwFUNCTION
        public CollectionViewSource vwFUNCTION
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFUNCTION");
            }
        }
        public DataRow dwFUNCTION
        {
            get
            {
                if (vwFUNCTION.View == null)
                    return null;
                if ((DataRowView)vwFUNCTION.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFUNCTION.View.CurrentItem).Row;
            }
        }
        #endregion


        #region=====vwICC_FUNCTIONS
        public CollectionViewSource vwICC_FUNCTIONS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPMENU");
            }
        }
        public DataRow dwEFUNCTION
        {
            get
            {
                if (vwICC_FUNCTIONS.View == null)
                    return null;
                if ((DataRowView)vwICC_FUNCTIONS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwICC_FUNCTIONS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPFUNCTION
        public CollectionViewSource vwTMPFUNCTION
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPFUNCTION");
            }
        }
        public DataRow dwTMPFUNCTION
        {
            get
            {
                if (vwTMPFUNCTION.View == null)
                    return null;
                if ((DataRowView)vwTMPFUNCTION.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPFUNCTION.View.CurrentItem).Row;
            }
        }
        #endregion

        #region=====vwTMPFOLDER=======
        public CollectionViewSource vwTMPFOLDER
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPFOLDER");
            }
        }
        public DataRow dwTMPFOLDER
        {
            get
            {
                if (vwTMPFOLDER.View == null)
                    return null;
                if ((DataRowView)vwTMPFOLDER.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPFOLDER.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwFOLDER=======
        public CollectionViewSource vwFOLDER
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFOLDER");
            }
        }
        public DataRow dwFOLDER
        {
            get
            {
                if (vwFOLDER.View == null)
                    return null;
                if ((DataRowView)vwFOLDER.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFOLDER.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwMENU=======
        public CollectionViewSource vwMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwMENU");
            }
        }
        public DataRow dwMENU
        {
            get
            {
                if (vwMENU.View == null)
                    return null;
                if (SetMaster.ICC_MENUDEF.Rows.Count == 0)
                    return null;
                if ((DataRowView)vwMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwMENU.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPMENU=======
        public CollectionViewSource vwTMPMENU
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPMENU");
            }
        }

        public DataRow dwTMPMENU
        {
            get
            {
                if (vwTMPMENU.View == null)
                    return null;
                if ((DataRowView)vwTMPMENU.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPMENU.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwTMPGABBS=======
        public CollectionViewSource vwTMPGABBS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPGABBS");
            }
        }
        public DataRow dwTMPGABBS
        {
            get
            {
                if (vwTMPGABBS.View == null)
                    return null;
                if ((DataRowView)vwTMPGABBS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPGABBS.View.CurrentItem).Row;
            }
        }
        #endregion
        #region=====vwGABBS=======
        public CollectionViewSource vwGABBS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwGABBS");
            }
        }
        public DataRow dwGABBS
        {
            get
            {
                if (vwGABBS.View == null)
                    return null;
                if ((DataRowView)vwGABBS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwGABBS.View.CurrentItem).Row;
            }
        }
        #endregion  

        DataSet setDISPO = new DataSet();
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public ExcelX.ExcelLib.ExcelAgent exAgent;
        ITXSETUPData m_Data;
        public string Mandant
        {
            get
            {
                return (string)dicParamater["MANDANT"];
            }
        }
        public string company
        {
            get
            {
                return (string)dicParamater["Company"];
            }
        }
        MainViewModel vm;
        public ITXSETUP()
        {
            InitializeComponent();
            this.Name = "参数设定";
            this.Title = "参数设定";
            //exAgent = new ExcelX.ExcelLib.ExcelAgent("");           
        }
        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            if (!IsSetUp)
            {
                ApplicationBase.Quit();
            }
            base.OnClosing(e); 
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded) return;
                IsLoaded = true;
                if (!IsSetUp)
                {
                    ApplicationBase = new ApplicationBase();
                    ApplicationBase.Load();
                    dicParamater = ApplicationBase.dicParamater;
                }
                OracleDB = (OracleConnection)dicParamater["OracleDB"];
                proxy = (IntexReptServBLClient)dicParamater["proxy"];
                
                vm = new MainViewModel();
                this.DataContext = vm;

                m_Data = new ITXSETUPData(OracleDB, proxy);
                SetEnv.Merge(m_Data.LoadEnv(Mandant));
                vwMENU.View.CurrentChanged += vwMENU_CurrentChanged;
                bgWoker = new BackgroundWorker();
                bgWoker.DoWork += bgWoker_DoWork;
                bgWoker.RunWorkerCompleted += bgWoker_RunWorkerCompleted;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void bgWoker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EventDispatcher.Instance.RaiseStatus("更新完成", StatusEventType.StopProgress);
        }

        void bgWoker_DoWork(object sender, DoWorkEventArgs e)
        { 
            EventDispatcher.Instance.RaiseStatus("开始更新", StatusEventType.StartProgress);
            m_Data.GetAllText2Translate("");
        }
   
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            try
            {
                IsSetUp = true;
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    base.SetUp(dicParamater);
                    this.dicParamater = dicParamater;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnGetCADMap_Click(object sender, RoutedEventArgs e)
        {
            DataSet setresult = null;
            try
            {
                SetMaster.Clear();

                setresult = m_Data.ReSetMaster();
                SetMaster.Merge(setresult);
                SetMaster.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                SetMaster.Merge(setresult);
                SetMaster.AcceptChanges();
            }
        }

        private void butDiscard_Click(object sender, RoutedEventArgs e)
        {
            SetDetail.Clear();
            SetDetail.AcceptChanges();
        }

       
    }
}
